import java.io.*;

public class SpareArray {
    public SpareArray() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        //创建二维数组
        int arrays[][] = new int[11][11];
        //模拟棋盘中的棋子
        arrays[1][2] = 1;
        arrays[2][3] = 2;
        arrays[3][4] = 1;
        arrays[4][1] = 2;
        for (int arr[] : arrays) {
            for (int data : arr) {
                System.out.print(data + "   ");
            }
            System.out.println();
        }
        //记录下总共有多少个数不是0
        int sum = 0;
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays.length; j++) {
                if (arrays[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println(sum);
        System.out.println();

        //创建稀疏数组
        //列为长度+1,行固定为3
        int sparesArray[][] = new int[sum + 1][3];
        sparesArray[0][0]=arrays.length;
        sparesArray[0][1]=arrays.length;
        sparesArray[0][2]=sum;
        int counts = 0 ;
        for (int i = 0 ; i<arrays.length;i++){
            for (int j =0; j<arrays.length;j++){
                if (arrays[i][j] != 0) {
                    counts++;
                    sparesArray[counts][0]=i;
                    sparesArray[counts][1]=j;
                    sparesArray[counts][2]=arrays[i][j];
                }
            }
        }
        //遍历
        for (int i = 0;i<sparesArray.length;i++){
            for (int j = 0 ; j<3 ; j++){
                System.out.print(sparesArray[i][j]+"  ");
            }
            System.out.println();
        }
        System.out.println();

        //将稀疏数组 -----> 数组
        int arr[][] = new int[sparesArray[0][0]][sparesArray[0][1]];

        for(int i =1;i<sparesArray.length;i++){
            arr[sparesArray[i][0]][sparesArray[i][1]]=sparesArray[i][2];
        }

        for (int[] ints : arr) {
            for (int data :ints){
                System.out.print(data+"  ");
            }
            System.out.println();
        }
        //将稀疏数组写到磁盘
        DataOutputStream is = null;
        try {
            is = new DataOutputStream(new FileOutputStream("F:\\111.txt"));
            for(int i =0;i<sparesArray.length;i++){
                for(int j = 0 ;j<3;j++){
                    is.writeInt(sparesArray[i][j]);
                    is.flush();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        System.out.println();



        //从磁盘中写出稀疏数组
        DataInputStream w = new DataInputStream(new FileInputStream("F:\\111.txt"));
        int countss=0;
        int [] [] spareArray2 = new int[sum+1][3];
        for (int i =0;i<spareArray2.length;i++){
            spareArray2[countss][0]= w.readInt();
            spareArray2[countss][1]= w.readInt();
            spareArray2[countss][2]= w.readInt();
            countss++;
        }
        for (int[] ints : spareArray2) {
            for(int data : ints){
                System.out.print(data);
            }
            System.out.println();
        }
        //将稀疏数组转换为二维数组
        int arr3 [] [] = new int[spareArray2[0][0]][spareArray2[0][1]];
        for (int i =1;i<spareArray2.length;i++){
            arr3[spareArray2[i][0]][spareArray2[i][1]]=spareArray2[i][2];
        }
        System.out.println();
        for (int[] ints : arr3) {
            for (int data :ints){
                System.out.print(data+"  ");
            }
            System.out.println();
        }
    }

}
